package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "C端-地址簿接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    @ApiOperation("新增地址")
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址,{}",addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> getAddressBookList(){
        log.info("查询当前登录用户的所有地址信息,{}", BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.selectListByUserId();
        return Result.success(list);
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefaultAddressBook(){
        log.info("查询默认地址,{}",BaseContext.getCurrentId());
        AddressBook addressBook = addressBookService.getDefaultAddressBook();
        return Result.success(addressBook);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddressByID(@PathVariable Long id){
        log.info("根据id查询地址,{}",id);
        AddressBook address = addressBookService.getAddressByID(id);
        return Result.success(address);
    }

    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook){
        log.info("根据id修改地址,{}",addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result updateAddressIsDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址,{}",addressBook);
        addressBookService.updateAddressIsDefault(addressBook);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteAddressBookById(Long id){
        log.info("根据id删除地址,{}",id);
        addressBookService.deleteAddressBookById(id);
        return Result.success();
    }

}
